package net.huashitong.ssydt.provider.exam.service;

import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.exam.entity.SsExamPaper;
import net.huashitong.ssydt.exam.entity.vo.ExamPaperEditInVO;
import net.huashitong.ssydt.exam.entity.vo.ExamPaperInVO;
import net.huashitong.ssydt.exam.entity.vo.ExamPaperQueryInVO;
import net.huashitong.ssydt.exam.entity.vo.PaperListInVO;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.provider.exam.mapper.SsExamPaperMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.04
 */
@Service
@Transactional
public class SsExamPaperServiceImpl implements SsExamPaperService {

    private SsExamPaperMapper mapper;

    @Autowired
    public SsExamPaperServiceImpl(SsExamPaperMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<SsExamPaper> query(PaperListInVO paperListInVO) {
        return mapper.query(paperListInVO);
    }

    @Override
    public SsExamPaper getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PaginationResult<SsExamPaper> paginationQuery(ExamPaperQueryInVO examPaperQueryInVO) {
        PaginationResult result = new PaginationResult();
        result.setPageNumber(examPaperQueryInVO.getPageNumber());
        result.setPageSize(examPaperQueryInVO.getPageSize());

        // 查询记录总数
        int total = mapper.paginationCount(examPaperQueryInVO);
        result.setPageRecords(total);

        if (total == 0) {
            result.setList(new ArrayList());
        } else {
            // 查询当前页记录
            List<SsExamPaper> list = mapper.paginationQuery(examPaperQueryInVO);
            result.setList(list);
        }
        return result;
    }

    @Override
    public void insertExamPaper(ExamPaperInVO examPaperInVO) {
        validateExamPaper(examPaperInVO);

        SsExamPaper ssExamPaper = new SsExamPaper();
        BeanUtils.copyProperties(examPaperInVO, ssExamPaper);
        ssExamPaper.setQuestionNumber(null);
        ssExamPaper.setTestNumber(null);

        mapper.insertSelective(ssExamPaper);
    }

    @Override
    public void updateExamPaper(ExamPaperEditInVO examPaperEditInVO) {
        validateExamPaper(examPaperEditInVO);

        SsExamPaper ssExamPaper = mapper.selectByPrimaryKey(examPaperEditInVO.getId());
        if (ssExamPaper == null) {
            throw new ServiceException("试卷不存在");
        }
        BeanUtils.copyProperties(examPaperEditInVO, ssExamPaper);
        ssExamPaper.setQuestionNumber(null);
        ssExamPaper.setTestNumber(null);
        ssExamPaper.setPaperProduceId(null);

        mapper.updateByPrimaryKeySelective(ssExamPaper);
    }

    @Override
    public void updatePaperQuestionNumber(long paperId, int questionNumber) {
        SsExamPaper ssExamPaper = new SsExamPaper();
        ssExamPaper.setId(paperId);
        ssExamPaper.setQuestionNumber(questionNumber);
        mapper.updateByPrimaryKeySelective(ssExamPaper);
    }

    @Override
    public int insert(SsExamPaper record) {
        return mapper.insert(record);
    }

    @Override
    public SsExamPaper getByPaperName(String paperName) {
        return mapper.getByPaperName(paperName);
    }

    @Override
    public void updateQuestionNumberAndCoefficient(SsExamPaper ssExamPaper) {
        mapper.updateQuestionNumberAndCoefficient(ssExamPaper);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<String> findByDistinctBelongYearMonth() {
        return mapper.findByDistinctBelongYearMonth();
    }

    private void validateExamPaper(ExamPaperInVO examPaperInVO) {
    }
}
